local GuildWarBottomPanel = app.mvc.createPanelClass(...)
local CrossServerGuildWarHelper = require("app.crossServer.views.guildWar.CrossServerGuildWarHelper")
local GuildWarRoundCountDown = import("..GuildWarRoundCountDown")

--data =
function GuildWarBottomPanel.createWithData(data, node)
    local sprite = GuildWarBottomPanel.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end

function GuildWarBottomPanel.create(node)
    return GuildWarBottomPanel.createWithData(nil, node)
end

function GuildWarBottomPanel:init(data)
    self.super.init(self, data)
    
    self._ui["Button_tips/Panel_1"]:addClickEventListener(function()
        app:sendMsg("CrossServerController", "openGuildWarReadMe")
    end)
    self._ui.Button_enter:addClickEventListener(handler(self, self._onClickBtnEnter))
    self:registerViewEvent({
        {name = ViewEvent.CSGuildWar_StageChange, callback = handler(self, self._onStageChange)},
       
    })
    self._ui["Panel_1/btn_tips"]:addClickEventListener(function()
        app:sendMsg("CrossServerController", "showJoinTips")
    end)
    
    local timeLine = self._ui["Button_tips"]:getTimeline()
    if timeLine then
        timeLine:gotoFrameAndPlay(0, true)
    end
    self:enableNodeEvents()
end

function GuildWarBottomPanel:onExit()
    self.super.onExit(self)
    self:_cleanCountDown()
  
end


function GuildWarBottomPanel:_onStageChange()
    print("=============+>>>  _onStageChange")
    self:refresh()
end

function GuildWarBottomPanel:_onClickBtnEnter()

    local name = self._ui["Button_enter/name"]:getString()
    if name == L("CSGuildWarBtnName2") then
        -- 进入战场
        UD.csGuildWarData:C2S_EnterWorldGuildWarMap()
    elseif name == L("CSGuildWarBtnName3") then
        -- 战绩榜
        app:sendMsg("CrossServerController", "popGuildWarGuildScore")
    end
end

function GuildWarBottomPanel:_cleanCountDown()
    if self._countDown then
        self._countDown:clean()
        self._countDown = nil
    end
end

function GuildWarBottomPanel:refresh()
    local csGuildWarData = UD.csGuildWarData
    if csGuildWarData:isMyGuildCanJoin() then
        self._ui["Button_enter"]:setVisible(true)
        local stage = csGuildWarData:getTodayStage()
        local matchInfo = csGuildWarData:getCurMatchInfo()
        assert(matchInfo ~= nil, "csGuildWarData matchInfo is nil")
        local guild1 = matchInfo:getGuildWithSort()
        assert(guild1 ~= nil, "guild1 is nil")
        self._ui.Panel_1:setVisible(true)
        self._ui.tips_1:setVisible(false)
        self._ui["Panel_1/name"]:setString(CrossServerGuildWarHelper:getStateStr(matchInfo:getRealRank(guild1)))
        if stage == Enums.CSGuildWarTodayStage.NotBegin then
            -- 暂未开始
            self._ui["Panel_1/btn_tips"]:setVisible(false)
            if matchInfo:getRound() >= 6 or (guild1:getWinCount() == 0 and guild1:getLoseCount() == 0) then
                self._ui["Panel_1/tips"]:setString(string.format(L("CSGuildWarFightResultTip1"), guild1:getWinCount(), guild1:getLoseCount()))
            else
                self._ui["Panel_1/tips"]:setString(string.format(L("CSGuildWarFightResultTip2"), guild1:getWinCount(), guild1:getLoseCount(), guild1:getRank()))
            end
            self._ui["Button_enter/name"]:setString(L("CSGuildWarBtnName3"))
        elseif stage == Enums.CSGuildWarTodayStage.GetTicketStart then
            self._ui["Panel_1/btn_tips"]:setVisible(false)
            self._ui["Panel_1/tips"]:setString(L("CSGuildWarCheckTicket"))
            self._ui["Button_enter/name"]:setString(L("CSGuildWarBtnName3"))
        elseif stage == Enums.CSGuildWarTodayStage.Fighting or stage == Enums.CSGuildWarTodayStage.GetTicketEnd then
            local ticketState = csGuildWarData:getTicketState()
            -- print("=======getTicketState=======>>>>  ", csGuildWarData:getTicketState(),ticketState, ticketState == Enums.CSGuildWarTicketState.Yes)
            if ticketState == Enums.CSGuildWarTicketState.Yes then
                self._ui["Panel_1/btn_tips"]:setVisible(false)
                if not self._countDown then
                    self._countDown = GuildWarRoundCountDown.new({
                        tickCallback = function(round, stage, nextBattTime, isStageChange)
                            local str = ""
                            if UD.csGuildWarData:isFightEnd() then
                                self:_cleanCountDown()
                            else
                                if round == 0 then
                                    if nextBattTime < 60 then
                                        str = string.format(L("CSGuildWarBattleCountDown"), nextBattTime)
                                    else
                                        str = string.format(L("CSGuildWarBattleCountDown_1"), math.floor( nextBattTime/60 ), nextBattTime%60)
                                    end
                                else
                                    str = string.format(L("CSGuildWarRoundCountDown"), nextBattTime)
                                end
                                self._ui["Panel_1/tips"]:setString(str) 
                            end
                            
                        end
                    })
                    self._countDown:startCountDown()
                end
                if csGuildWarData:isCSGuildWarPrepareMap() then
                    self._ui["Button_enter"]:setVisible(false)
                else
                    self._ui["Button_enter/name"]:setString(L("CSGuildWarBtnName2"))
                end
            else
                self._ui["Panel_1/btn_tips"]:setVisible(true)
                self._ui["Panel_1/tips"]:setString(L("CSGuildWarNoTicketTip"))
                local width1 = self._ui["Panel_1/name"]:getVirtualRendererSize().width
                local width2 = self._ui["Panel_1/tips"]:getVirtualRendererSize().width
                local maxWidth = math.max( width1, width2 )

                self._ui["Panel_1/btn_tips"]:setPositionX(self._ui["Panel_1/name"]:getPositionX() + maxWidth/2 + 30)
                self._ui["Button_enter/name"]:setString(L("CSGuildWarBtnName3"))
            end
        else
            self._ui["Panel_1/btn_tips"]:setVisible(false)
            if CrossServerGuildWarHelper.isCurSeasonEnd() then
                -- 星期天  赛季结束
                self._ui["Panel_1/name"]:setString(L("WGWSeasonIsOver"))
                self._ui["Panel_1/tips"]:setString(string.format(L("WGWFinialRank"), matchInfo:getRealRank(guild1)))
            else
                self._ui["Panel_1/tips"]:setString(L("CSGuildWarTodayStageEnd"))
            end
            self._ui["Button_enter/name"]:setString(L("CSGuildWarBtnName3"))
        end
        local errorStr = matchInfo:getError()
        if errorStr then
            self._ui["Panel_1/tips"]:setString(errorStr)
        end
    else
        self._ui.Panel_1:setVisible(false)
        self._ui.tips_1:setVisible(true)

        local guildId = UD:getGuildId()
        if guildId and guildId ~= 0 then
            self._ui.tips_1:setString(CrossServerGuildWarHelper.getGuildCanNotJoinTitle())
        else
            self._ui.tips_1:setString(L("WGWPlayerHasNoGuild"))
        end
        self._ui["Button_enter"]:setVisible(false)--:setString(L("CSGuildWarBtnName1"))
    end
    
end

return GuildWarBottomPanel